﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace School.Pages.Admission
{
    public partial class ImageUploadPage : System.Web.UI.Page
    {
        int studentid;
        protected void Page_Load(object sender, EventArgs e)
        {
            studentid = int.Parse(Request.QueryString.Get("crl1").ToString());
            if (System.IO.File.Exists(Server.MapPath("~/StudentPics/") + studentid + ".jpg") == true)
            {
                Image1.ImageUrl = "~//StudentPics//" + studentid + ".jpg";
            }
        }
        protected void Upload_File(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                try
                {
                    if (FileUpload1.PostedFile.ContentType == "image/jpeg" || FileUpload1.PostedFile.ContentType == "image/pjpeg")
                    {
                        if (FileUpload1.PostedFile.ContentLength < 1048578)
                        {
                            string filename = Path.GetFileName(FileUpload1.FileName);
                            FileUpload1.SaveAs(Server.MapPath("~/StudentPics/") + studentid + ".jpg");
                            lblmessage.Text = "تم رفع الصورة بنجاح";
                            Image1.ImageUrl = "~//StudentPics//" + studentid + ".jpg";
                        }
                        else
                            lblmessage.Text = "حجم الصورة أكبر من 2 ميجا ";
                    }
                    else
                        lblmessage.Text = "فقط jpg الملفات من نوع ";
                }
                catch (Exception ex)
                {
                    lblmessage.Text = "لم يتم رفع الصورة" + ex.Message;
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string strbScript;

            strbScript = "<script language='javascript'>";
            strbScript = strbScript + "window.opener.document.getElementById('" + Request.QueryString.Get("crl2").ToString() + "').src='../StudentPics/" + studentid + ".jpg' ;";
            strbScript = strbScript + "window.close();";
            strbScript = strbScript + "</script>";
            Response.Write(strbScript);
        }
    }
}